import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'getFromList'
})
export class GetFromListPipe implements PipeTransform {
  transform(val: any, key: string, value: string) {
    if (Array.isArray(val)) {
      for (let i = 0; i < val.length; i++) {
        val[i][key] = this.trim(val[i][key]);
        val[i]['value'] = this.trim(val[i]['value']);
        if (val[i][key] === value) {
          return { item: i, value: val[i] };
        }
      }
    }
    return null;
  }

  // 去左右空格;
  trim(s) {
    return s.replace(/(^\s*)|(\s*$)/g, "");
  }
}
